<?php

namespace app\admin\controller;

use app\admin\common\Adminbase;
use service\TreeService;
use app\admin\model\User as Usermodel;
use think\Db;

class User extends Adminbase
{
    protected $user_model;

    protected function _initialize(){
        parent::_initialize();
        $this->user_model = new Usermodel();
    }

    public function index(){
        if($this->request->isAjax()) {
            $param = $this->request->param();//接收参数
            //dump($param);exit;
            /**
             * 返回获取的分页数据
             */
            return $this->user_model->GetUsersDataPage($param);
        }

        //获取集团总部部门
        $fields = "*,department_id as id";
        $deparmentlists = Db::name("department")->field($fields)
            //->where(['is_jtzb'=>1])
            ->where(['status'=>1])->order("sort asc")->select();
        /*foreach ($deparmentlists as &$row){
            if($row['pid'] == 10000000){
                $row['pid'] = 0;
            }
            //$row['spread'] = true;
        }*/
        //dump($deparmentlists);exit;
        $data = json_encode(TreeService::DeepTree($deparmentlists),true);
        $this->assign("data", $data);

        return $this->fetch();
    }

    public function setfloor(){
        if($this->request->isAjax()) {
            $data = $this->request->param();
            //dump($data);exit;
            if(isset($data['floorinfo'])){
                Db::name("user")->where(['id'=>$data['id']])->update(['floorinfo'=>implode(",",$data['floorinfo'])]);
                $this->success("提交成功");
            }
            else{
                $this->error("请选择电梯楼层");
            }

        }
        $id = $this->request->param("id/d");
        $info = Db::name("user")->where(['id'=>$id])->find();
        $info['floorinfo'] = explode(",",$info['floorinfo']);
        $this->assign("info",$info);
        //dump($info);exit;
        return $this->fetch();
    }

}